package small.common.core.thread.executor;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class NameThreadFactory implements ThreadFactory {
    private final AtomicInteger id = new AtomicInteger(0);
    private String name;

    public NameThreadFactory(String name) {
        if (!name.endsWith(".")) {
            name = name + ".";
        }

        this.name = name;
    }

    public Thread newThread(Runnable r) {
        String threadName = this.name + this.id.getAndIncrement();
        Thread thread = new Thread(r, threadName);
        thread.setDaemon(true);
        return thread;
    }
}